package com.kcsoft.controller;

import com.kcsoft.pojo.*;
import com.kcsoft.service.AdminService;
import com.kcsoft.service.ChatService;
import com.kcsoft.service.UserService;
import com.kcsoft.utils.Md5Util;
import com.kcsoft.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.naming.Name;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    UserService userService;

    @Autowired
    ChatService chatService;

    private void updateUserProperties(User user, String stuId, Integer role, Integer classId, String name) {
        if (stuId != null) {
            user.setStuId(stuId);
        }
        if (role != null) {
            user.setRole(role);
        }
        if (classId != null) {
            user.setClassId(classId);
        }
        if (name != null) {
            user.setName(name);
        }
    }

    @PostMapping ("/add")
    public Result add(String username,
                      String password,
                      @RequestParam(required = false) String stuId,
                      @RequestParam(required = false) Integer role,
                      @RequestParam(required = false) Integer classId){

        User user = userService.findByUsername(username);

        if (user == null) {

            String pwd= Md5Util.genPwd(password);

            adminService.add(username, pwd, stuId, role, classId);

            return Result.success();

        }

        return Result.error("用户名已存在");

    }

    @PostMapping("/update")
    public Result updateRole(@RequestParam String username,
                             @RequestParam(required = false) String password,
                             @RequestParam(required = false) String stuId,
                             @RequestParam(required = false) Integer role,
                             @RequestParam(required = false) Integer classId,
                             @RequestParam(required = false) String name){

        User user = userService.findByUsername(username);

        if (user != null){
            if (password != null){
                String pwd = Md5Util.genPwd(password);
                user.setPassword(pwd);
            }
            updateUserProperties(user, stuId, role, classId, name);
            adminService.updateUser(user);
            return Result.success();
        }
        return Result.error("用户不存在");

    }

    @PostMapping("/delete")
    public Result delete(Integer id) {
        adminService.deleteUser(id);
        return Result.success();
    }

    @GetMapping("/assistants")
    public Result<List<Object>> getAssistants(){
        return chatService.getAssistants();
    }

    @PostMapping("/addModel")
    public Result addModel(@RequestBody Assistant assistant) {
        System.out.println(assistant.toString());
        adminService.addModel(assistant.getName(), assistant.getAssistant(), assistant.getValue(), assistant.getDescription(), assistant.getType());
        return Result.success();
    }

    @PostMapping("/delModel")
    public Result delModel(Integer id) {
        adminService.delModel(id);
        return Result.success();
    }

    @PostMapping("/listClasses")
    public Result<PageBean<Classes>> listClasses(Integer pageNum, Integer pageSize) {
        PageBean<Classes> pb = adminService.listClasses(pageNum, pageSize);
        return Result.success(pb);
    }

}
